package com.ghline.caloncabe.model;

import java.util.Random;

import com.ghline.caloncabe.GameView;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Sprite {
	
	private static final int BMP_ROWS = 2;
    private static final int BMP_COLUMNS = 2;
    private int x = 0;
    private int y = 0;
    private int xSpeed = 5;
    private int ySpeed;
    private GameView gameView;
    private Bitmap bmp;
    private int currentFrame = 0;
    private int width;
    private int height;
    private static final int MAX_SPEED = 3;
    private int addSpeed = 1;
	
	public Sprite(GameView gameView, Bitmap bmp) {
		
		this.gameView = gameView;
        this.bmp = bmp;
        this.width = bmp.getWidth() / BMP_COLUMNS;
        this.height = bmp.getHeight() / BMP_ROWS;
        
        Random rnd = new Random();
        x = rnd.nextInt(gameView.getWidth() - this.width);
        y = rnd.nextInt(gameView.getHeight() - this.height);
        xSpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED + getAddSpeed();
        ySpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED + getAddSpeed();
	}
	
	public Sprite(GameView gameView, Bitmap bmp, int sy) {
		this.gameView = gameView;
        this.bmp = bmp;
        this.width = bmp.getWidth() / BMP_COLUMNS;
        this.height = bmp.getHeight() / BMP_ROWS;
        
        Random rnd = new Random();
        this.x = rnd.nextInt(gameView.getWidth() - this.width);
        this.y = sy;        
        
        xSpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED + getAddSpeed();
        ySpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED + getAddSpeed();
	}
	
	private void update() {
		if (x >= gameView.getWidth() - width - xSpeed || x + xSpeed <= 0) {
            xSpeed = -xSpeed;
		}
	    x = x + xSpeed;	
	    if (y >= gameView.getHeight() - height - ySpeed || y + ySpeed <= 0) {
	        ySpeed = -ySpeed;
	    }
	    y = y + ySpeed;
	    currentFrame = ++currentFrame % BMP_ROWS;
	}

	public void draw(Canvas canvas) {	
		
		update();		
        int srcX = getDirection() * width;
        int srcY = currentFrame * height;
        Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
        Rect dst = new Rect(x, y, x + width, y + height);
        canvas.drawBitmap(bmp, src, dst, null);
	}
	
	private int getDirection() {
		if (xSpeed < 0) {
			return 0;
		}
		return 1;		
	}
	
	public boolean isCollition(float x2, float y2) {
        return x2 > x && x2 < x + width && y2 > y && y2 < y + height;
	}
	
	public boolean collidesWith(Sprite sprite) {
		
		int left1, left2;
		int right1, right2;
		int top1, top2;
		int bottom1, bottom2;
		
		left1 = this.x;
		left2 = sprite.x;
		right1 = left1 + this.width;
		right2 = left2 + sprite.width;
		top1 = this.y;
		top2 = sprite.y;
		bottom1 = top1 + this.height;
		bottom2 = top2 + sprite.height;
		
		if (bottom1 < top2)
			return false;
		if (top1 > bottom2)
			return false;
		if (right1 < left2)
			return false;
		if (left1 > right2)
			return false;				
		return true;
	}
	public int getWidth() {
		return width;
	}
	public int getHeight() {
		return height;
	}
	public int getX() {
		return x;
	}
	public int getY() {
		return y;
	}
	public Bitmap getBitmap() {
		return bmp;
	}
	public void setBitmap(Bitmap bmp) {
		this.bmp = bmp;
	}

	public int getAddSpeed() {
		return addSpeed;
	}

	public void setAddSpeed(int addSpeed) {
		this.addSpeed = addSpeed;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void setX(int x) {
		this.x = x;
	}

	public void setY(int y) {
		this.y = y;
	}
}
